<?php
class ControllerModuleArticleByType extends Controller {
	protected function index($setting) {	
		static $module = 0;
		$this->language->load('module/article_by_type'); 
		$this->load->model('news/article'); 		
		$this->load->model('news/utf8'); 
		$this->load->model('tool/image');
		
      	$this->data['heading_title'] = $this->language->get('text_'.$setting['article_type']);
		$this->data['limit'] = $setting['limit'];
		$this->data['width'] = $setting['image_width'];
		$this->data['height'] = $setting['image_height'];
		$this->data['text_related'] = $this->language->get('text_related');	
        $this->data['text_date_added'] = $this->language->get('text_date_added');
		$this->data['text_author'] = $this->language->get('text_author');
		$this->data['text_comments'] = $this->language->get('text_comments');
		$this->data['text_more'] = $this->language->get('text_more');
		if($setting['description_limit']){
		$description_limit=(int)$setting['description_limit'];
		}elseif($this->config->get('article_description_limit')){
		$description_limit=$this->config->get('article_description_limit');
		}			else{
			$description_limit=255;
		}
		

		$this->data['articles'] = array();
		/*article type*/ 
		/*featured_article*/ 
		if ($setting['article_type']=='featured_article') {
				$articles = $this->config->get('featured_article');	
				if (empty($setting['limit'])) {
					$setting['limit'] = 5;
				}
				$articles = array_slice($articles, 0, (int)$setting['limit']);
				foreach ($articles as $article_id) {
					$article_info = $this->model_news_article->getArticle($article_id);		
									
					if ($article_info) {
					$related_articles = array();
						
						$related = $this->model_news_article->getArticleRelatedLimit($article_info['article_id'],3);	
						foreach ($related as $relate) {							
							$related_articles[] = array(
								'article_id' => $relate['article_id'],
								'name'    	 => $relate['name'],
								'href'    	 => $this->url->link('news/article', 'article_id=' . $relate['article_id']),
							);
						}	
						if ($article_info['image']) {
							$image = $this->model_tool_image->resize($article_info['image'], $setting['image_width'], $setting['image_height']);
						} else {
							$image = false;
						}
						
						if ($this->config->get('config_comment_status')) {
							$rating = $article_info['rating'];
						} else {
							$rating = false;
						}
								
					$this->data['articles'][] = array(
						'article_id' => $article_info['article_id'],
						'date_added' => date($this->config->get('config_article_date_format'), strtotime($article_info['date_added'])),
						 'related' => $related_articles,
						'thumb'   	 => $image,
						'name'    	 => $article_info['name'],
						'description' => $this->model_news_utf8->utf8_substr(strip_tags(html_entity_decode($article_info['description'], ENT_QUOTES, 'UTF-8')), 0, $description_limit) . '..',
						'rating'     => $rating,
						'author'        => $article_info['author'],
						'author_href'    	 => $this->url->link('news/author', 'author_id=' . $article_info['author_id']),
						'comments'    => sprintf($this->language->get('text_comments'), (int)$article_info['comments']),
						'viewed'    => sprintf($this->language->get('text_viewed'), (int)$article_info['viewed']),
						'href'    	 => $this->url->link('news/article', 'article_id=' . $article_info['article_id']),
					);
				}
			}
		}
		if ($setting['article_type']!='featured_article') {
		/*recent_article*/ 
		if ($setting['article_type']=='recent_article') {
		$data = array(
					'sort'  => 'p.date_added',
					'order' => 'DESC',
					'start' => 0,
					'limit' => $setting['limit']
				);
		
				$articles = $this->model_news_article->getArticles($data);
		}
		/*most_viewed*/ 
		if ($setting['article_type']=='most_viewed') {
		$data = array(
			'sort'  => 'p.viewed',
			'order' => 'DESC',
			'start' => 0,
			'limit' => $setting['limit']
		);
		$articles = $this->model_news_article->getArticles($data);
		}
		/*article type*/ 
				foreach ($articles as $result) {
				
					$related_articles = array();
						
					$related = $this->model_news_article->getArticleRelatedLimit($result['article_id'],3);	
					foreach ($related as $relate) {							
						$related_articles[] = array(
							'article_id' => $relate['article_id'],
							'name'    	 => $relate['name'],
							'href'    	 => $this->url->link('news/article', 'article_id=' . $relate['article_id']),
						);
					}	
					if ($result['image']) {
						$image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
					} else {
						$image = false;
					}
	
			
			if ($this->config->get('config_comment_status')) {
				$rating = $result['rating'];
			} else {
				$rating = false;
			}
			
			$this->data['articles'][] = array(
				'article_id' => $result['article_id'],
				'date_added' => date($this->config->get('config_article_date_format'), strtotime($result['date_added'])),
				'related' => $related_articles,
				'thumb'   	 => $image,
				'name'    	 => $result['name'],
				'description' => $this->model_news_utf8->utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $description_limit) . '..',
				'rating'     => $rating,
				'author'        => $result['author'],
				'author_href'    	 => $this->url->link('news/author', 'author_id=' . $result['author_id']),
				'comments'    => sprintf($this->language->get('text_comments'), (int)$result['comments']),
				'viewed'     => sprintf($this->language->get('text_viewed'), (int)$result['viewed']),
				'href'    	 => $this->url->link('news/article', 'article_id=' . $result['article_id']),
			);
		}
		}/*end !=*/ 
		
		$this->data['module'] = $module++; 
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/article_by_type.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/article_by_type.tpl';
		} else {
			$this->template = 'default/template/module/article_by_type.tpl';
		}

		$this->render();
	}
}
?>